let requestUrl = null;

function addHandler() {
	requestUrl = "/chongwu_neidan_category/insert";

	$("#neidan_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#neidan_list").datagrid("getSelected");
	
	if(rowData) {
		requestUrl = "/chongwu_neidan_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#note").textbox("setValue", rowData.note);
		$("#template").textbox("setValue", rowData.template);

		$("#neidan_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#neidan_list").datagrid("getSelected");
	
	if(rowData) {
		$("#neidanId").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function downloadHandler() {
	location.href = "/chongwu_neidan_category/download";
}

$(document).ready(function() {

	$("#_name_").textbox({
		width: 150,
		prompt: "输入内丹名称"
	});

	$("#_type_").combobox({
		url: "/chongwu_neidan_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-内丹类型-",
		panelHeight: "auto"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let name = $("#_name_").textbox("getValue");
		let type = $("#_type_").combobox("getValue");

		$("#neidan_list").datagrid("reload", {
			name: name,
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#type").combobox({
		url: "/chongwu_neidan_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto"
	});

	$("#note").textbox({
		width: 375,
		height: 80,
		required: true,
		multiline: true
	});

	$("#template").textbox({
		width: 375,
		height: 80,
		multiline: true
	});

	$("#neidan_dialog").dialog({
		title: "宠物内丹",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#neidan_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#neidan_dialog").dialog("close");
						$("#neidan_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#neidan_form").form("clear");
				$("#neidan_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#neidan_list", "/chongwu_neidan_category/upload");

	fetchUploadDialog("/chongwu_neidan_category/updateImageById", function () {
		$("#neidan_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#neidan_list").datagrid({
		url: "/chongwu_neidan_category/selectByPage",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		onHeaderContextMenu: function(e) {
			e.preventDefault();

			if (!columnMenu){
				createColumnMenu("#neidan_list");
			}
			columnMenu.menu("show", {
				left: e.pageX,
				top: e.pageY
			});
		},
		onLoadSuccess: function () {
			let selector = "#neidan_list";
			let fields = getFields(selector);

			if (fields.length > 0) {
				let datagrid = $(selector);

				for (let i = 0; i < fields.length; i++) {
					let field = fields[i];

					// 隐藏未选中的字段
					if (!field.selected) {
						datagrid.datagrid("hideColumn", field.name);
					}
				}

				datagrid.datagrid("fitColumns");
			}
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-download",
			text: "下载图片",
			handler: function() {
				downloadHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "内丹名称", align: "center", width: 100},
			{field: "type", title: "内丹类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chongwu_neidan_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "image", title: "图片", align: "center", width: 100
				, formatter: function(value) {
					return "<img src='" + value + "' height='" + size + "' />";
				}
			},
			{field: "note", title: "基础效果", width: 500, align: "center"
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "template", title: "说明模板", width: 500, align: "center"
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});